package cn.lazyturtle.bugpush.config;

import org.jooq.ExecuteContext;
import org.jooq.SQLDialect;
import org.jooq.impl.DefaultExecuteListener;
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;
import org.springframework.jdbc.support.SQLExceptionTranslator;
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator;

import java.util.Objects;

/**
 * @author Administrator
 */
public class JooqToSpringExceptionTransformer extends DefaultExecuteListener {
    @Override
    public void exception(ExecuteContext ctx) {
        SQLDialect dialect = ctx.configuration().dialect();
        SQLExceptionTranslator translator = new SQLErrorCodeSQLExceptionTranslator(dialect.name());

        String task = "jOOQ";
        ctx.exception(translator.translate(task, ctx.sql(), Objects.requireNonNull(ctx.sqlException())));
    }
}
